package com.atguigu.mall.ware.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.atguigu.common.to.SkuHasStockVO;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.mall.ware.dao.WareSkuDao;
import com.atguigu.mall.ware.entity.WareSkuEntity;
import com.atguigu.mall.ware.service.WareSkuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String wareIdStr = (String) params.get("wareId");
        String skuIdStr = (String) params.get("skuId");
        LambdaQueryWrapper<WareSkuEntity> wareSkuEntityQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(wareIdStr)){
            wareSkuEntityQueryWrapper.eq(WareSkuEntity::getWareId,Long.parseLong(wareIdStr));
        }
        if (StrUtil.isNotBlank(skuIdStr)){
            wareSkuEntityQueryWrapper.eq(WareSkuEntity::getSkuId,Long.parseLong(skuIdStr));
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wareSkuEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuHasStockVO> getSkusHasStock(List<Long> skus) {
        List<SkuHasStockVO> result = new ArrayList<>();
        LambdaQueryWrapper<WareSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(WareSkuEntity::getSkuId,skus);
        List<WareSkuEntity> list = this.list(wrapper);
        if (CollUtil.isNotEmpty(list)){
            Map<Long, List<WareSkuEntity>> skuWareMap = list.stream().collect(Collectors.groupingBy(WareSkuEntity::getSkuId));
            skuWareMap.forEach((skuId,wareList)->{
                Long sum = wareList.stream()
                        .map(WareSkuEntity::getStock)
                        .collect(Collectors.summarizingInt(Integer::intValue)).getSum();
                if (ObjectUtil.isNull(sum)){
                    sum = 0L;
                }
                Boolean hasStock = sum>0L;
                SkuHasStockVO skuHasStockVO = new SkuHasStockVO();
                skuHasStockVO.setSkuId(skuId);
                skuHasStockVO.setHasStock(hasStock);
                result.add(skuHasStockVO);
            });
        }else {
          return  skus.stream().map(skuId -> {
                SkuHasStockVO skuHasStockVO = new SkuHasStockVO();
                skuHasStockVO.setSkuId(skuId);
                skuHasStockVO.setHasStock(false);
                return skuHasStockVO;
            }).collect(Collectors.toList());
        }
        return result;
    }

}